package cp.group;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

public class Test {
    public static void main(String[] args) throws IOException {
        String url = "https://pic.netbian.com/";
        HttpsUrlValidator.retrieveResponseFromServer(url);
        Document document = Jsoup.parse(new URL(url), 10000);
        Element main = document.getElementById("main");
        //System.out.println(main);
        Elements slist = main.getElementsByClass("slist");
        Element element = slist.get(0);
       // System.out.println(element);
        Elements imgs = element.getElementsByTag("img");
        List<String> listTitles = new ArrayList<String>();
        Elements as = element.getElementsByTag("a");
        for (Element  a : as){
            listTitles.add(a.attr("title").substring(0, 6));
        }
       // System.out.println(as);
        for (int i = 0;i<imgs.size();i++){
            Element img = imgs.get(i);
            String src = img.attr("src");
            src = url + src;
            //获取输入流
            URL target = new URL(src);
            URLConnection urlConnection = target.openConnection();
            InputStream inputStream = urlConnection.getInputStream();
            String name = listTitles.get(i);
            OutputStream outputStream = new FileOutputStream("D:\\paichong-imgs\\"+name+".jpg");
            int temp = 0;
            while ((temp=inputStream.read())!=-1){
                outputStream.write(temp);
            }
            System.out.println(name+".jpg 下载完毕! ");
            outputStream.close();
            inputStream.close();
        }
    }
}
